#include "comm/znx_pool.h"
#include "comm/znx_mem.h"

void test_pool_mem_alloc(znx_pool_t *pool)
{
    void *p;
    p = znx_pool_malloc(pool, 1);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 100);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 900);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 900);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 900);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 900);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 328);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 900);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 900);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 900);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 1024);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 1024);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 4096);
    znx_pool_free(pool, p);
    p = znx_pool_malloc(pool, 8192);

    p = znx_pool_block_malloc(pool, 100);
}

static inline void
znx_test_cleanup(void *data)
{
    DD("cleanup %d", (int)(intptr_t)data);
}

void
test_pool_cleanup(znx_pool_t *pool) 
{
    znx_cleanup_item_t *item1 = znx_pool_add_cleanup(pool, znx_test_cleanup, (void *)1);
    znx_cleanup_item_t *item2 = znx_pool_add_cleanup(pool, znx_test_cleanup, (void *)2);
    znx_cleanup_item_t *item3 = znx_pool_add_cleanup(pool, znx_test_cleanup, (void *)3);
    znx_pool_add_cleanup(pool, znx_test_cleanup, (void *)4);
    znx_pool_add_cleanup(pool, znx_test_cleanup, (void *)5);
    znx_pool_del_cleanup(pool, item1);
    znx_pool_del_cleanup(pool, item2);
    znx_pool_del_cleanup(pool, item3);
    znx_pool_add_cleanup(pool, znx_test_cleanup, (void *)6);
    znx_pool_add_cleanup(pool, znx_test_cleanup, (void *)7);
    znx_pool_add_cleanup(pool, znx_test_cleanup, (void *)8);
}

int main() {
    znx_pool_t *pool = znx_pool_create();

    test_pool_mem_alloc(pool);

    test_pool_cleanup(pool);

    znx_pool_destroy(pool);

    return 0;
}